package com.edu.buservice.biz.covert;

import com.edu.buservice.biz.model.Request.QueryUserRequest;
import com.edu.buservice.biz.model.Request.UserRequest;
import com.edu.buservice.biz.model.dto.UpdateUserInfoDTO;
import com.edu.buservice.dal.model.SysPermissions;
import com.edu.buservice.dal.model.SysRoles;
import com.edu.buservice.dal.model.SysUsers;
import com.edu.buservice.facade.model.response.BaseUserInfoDTO;
import com.edu.buservice.util.model.UserInfoDTO;
import com.edu.buservice.util.model.enums.ErrorCodeEnum;
import com.edu.buservice.util.model.error.BizException;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @program: KQAIA
 * @description:
 * @author: 无恙
 * @create: 2025-03-09 13:30
 **/


public class UserInfoCovert {

	public static UserInfoDTO convertUserInfoDTO(SysUsers sysUsers, SysRoles sysRoles, List<SysPermissions> sysPermissionsList) {
		if (sysUsers == null || sysRoles == null) {
			throw new BizException(ErrorCodeEnum.USER_NOT_FOUND, "用户不存在");
		}

		UserInfoDTO userInfoDTO = new UserInfoDTO();
		userInfoDTO.setUserId(sysUsers.getUserId());
		userInfoDTO.setUsername(sysUsers.getUsername());
		userInfoDTO.setPhone(sysUsers.getPhone());
		userInfoDTO.setAvatarUrl(sysUsers.getAvatarUrl());
		userInfoDTO.setEmail(sysUsers.getEmail());
		userInfoDTO.setStatus(sysUsers.getStatus());

		userInfoDTO.setRole(sysRoles.getName());

		Set<String> permissions = Collections.emptySet();
		if (sysPermissionsList != null && !sysPermissionsList.isEmpty()) {
			permissions = sysPermissionsList.stream()
					.map(SysPermissions::getPermissionCode)
					.collect(Collectors.toSet());
		}
		userInfoDTO.setPermissions(permissions);

		return userInfoDTO;
	}

	public static SysUsers CovertByUserRequest(UserRequest userRequest){
		SysUsers sysUsers = new SysUsers();
		sysUsers.setUsername(userRequest.getUserName());
		sysUsers.setPhone(userRequest.getPhone());
		sysUsers.setEmail(userRequest.getEmail());
		sysUsers.setPassword(userRequest.getPassword());
		sysUsers.setAvatarUrl(userRequest.getAvatarUrl());
		sysUsers.setTenantId(userRequest.getTenantId());
		return sysUsers;
	}


	public static BaseUserInfoDTO covertToUserInfo(SysUsers sysUsers){
		BaseUserInfoDTO baseUserInfoDTO = new BaseUserInfoDTO();
		baseUserInfoDTO.setUserId(sysUsers.getUserId());
		baseUserInfoDTO.setUsername(sysUsers.getUsername());
		baseUserInfoDTO.setPhone(sysUsers.getPhone());
		baseUserInfoDTO.setAvatarUrl(sysUsers.getAvatarUrl());
		baseUserInfoDTO.setEmail(sysUsers.getEmail());
		baseUserInfoDTO.setStatus(sysUsers.getStatus());
		baseUserInfoDTO.setTenantId(sysUsers.getTenantId());
		return baseUserInfoDTO;
	}

	public static SysUsers convertToSysUsers(QueryUserRequest queryUserRequest){
		SysUsers sysUsers = new SysUsers();

		Optional.ofNullable(queryUserRequest.getUserId()).ifPresent(sysUsers::setUserId);

		Optional.ofNullable(queryUserRequest.getPhone()).ifPresent(sysUsers::setPhone);
		Optional.ofNullable(queryUserRequest.getName()).ifPresent(sysUsers::setUsername);

		Optional.ofNullable(queryUserRequest.getTenantId()).ifPresent(sysUsers::setTenantId);

		Optional.ofNullable(queryUserRequest.getStatus()).ifPresent(s -> sysUsers.setStatus(Integer.parseInt(s)));
		return sysUsers;
	}

	public static SysUsers convertToSysUsers(UpdateUserInfoDTO updateUserInfoDTO){
		SysUsers sysUsers = new SysUsers();

		Optional.ofNullable(updateUserInfoDTO.getUserId()).ifPresent(sysUsers::setUserId);
		Optional.ofNullable(updateUserInfoDTO.getPhone()).ifPresent(sysUsers::setPhone);
		Optional.ofNullable(updateUserInfoDTO.getUsername()).ifPresent(sysUsers::setUsername);
		Optional.ofNullable(updateUserInfoDTO.getTenantId()).ifPresent(sysUsers::setTenantId);
		Optional.ofNullable(updateUserInfoDTO.getPassword()).ifPresent(sysUsers::setPassword);
		Optional.ofNullable(updateUserInfoDTO.getEmail()).ifPresent(sysUsers::setEmail);
		Optional.ofNullable(updateUserInfoDTO.getStatus()).ifPresent(sysUsers::setStatus);
		Optional.ofNullable(updateUserInfoDTO.getAvatarUrl()).ifPresent(sysUsers::setAvatarUrl);
		Optional.ofNullable(updateUserInfoDTO.getLastLoginIp()).ifPresent(sysUsers::setLastLoginIp);
		Optional.ofNullable(updateUserInfoDTO.getLastLoginTime()).ifPresent(sysUsers::setLastLoginTime);
		return sysUsers;
 	}

}
